package com.sanrenzu.everybodytv.ui.recommend;

import android.os.Handler;

import com.sanrenzu.everybodytv.bean.sms.BannerBean;
import com.sanrenzu.everybodytv.bean.sms.CateBean;
import com.sanrenzu.everybodytv.bean.sms.RecBean;
import com.sanrenzu.everybodytv.bean.sms.RecSubBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by asus on 2016/9/6.
 */
public class RecommendPresenter implements RecommendContract.Presenter {

    private RecommendContract.View view;
    private RecommendContract.Model model;
    private Handler handler = new Handler();
    private List<RecSubBean> list = new ArrayList<>();
    private List<BannerBean> bannerListFromString= new ArrayList<>();

    public RecommendPresenter(RecommendContract.View view){
        this.view = view;
        model = new RecommendModel();
    }

    @Override
    public void getRecommendBean() {
        //ListView的
        model.getRecommend(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String json = response.body().string();
                    list = new ArrayList<>();
                    //解析lol
                    List<RecBean> listFromString = getListFromString(json, "app-recommendation");
                    list.add(new RecSubBean("精彩推荐",listFromString ));
                    list.add(new RecSubBean("英雄联盟",getListFromString(json,"app-lol")));
                    list.add(new RecSubBean("全民星秀",getListFromString(json,"app-beauty")));
                    list.add(new RecSubBean("炉石传说",getListFromString(json,"app-heartstone")));
                    list.add(new RecSubBean("全民户外",getListFromString(json,"app-huwai")));
                    list.add(new RecSubBean("守望先锋",getListFromString(json,"app-overwatch")));
                    list.add(new RecSubBean("暴雪经典",getListFromString(json,"app-blizzard")));
                    list.add(new RecSubBean("体育频道",getListFromString(json,"app-sport")));
                    list.add(new RecSubBean("QQ飞车",getListFromString(json,"app-qqfeiche")));
                    list.add(new RecSubBean("手游专区",getListFromString(json,"app-mobilegame")));
                    list.add(new RecSubBean("王者荣耀",getListFromString(json,"app-wangzhe")));
                    list.add(new RecSubBean("Dota2",getListFromString(json,"app-dota2")));
                    list.add(new RecSubBean("单机主机",getListFromString(json,"app-tvgame")));
                    list.add(new RecSubBean("网络游戏",getListFromString(json,"app-webgame")));
                    list.add(new RecSubBean("DNF",getListFromString(json,"app-dnf")));
                    list.add(new RecSubBean("我的世界",getListFromString(json,"app-minecraft")));
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            view.onGetRecommendSuccess(list);
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
        //ViewPager的
        model.getRecommendViewPager(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String json = response.body().string();
                    bannerListFromString = getBannerListFromString(json, "app-focus");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onGetRecommendViewPager(bannerListFromString);
                    }
                });
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
        //RecyclerView的
        model.getRecommendRecycleView(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                final List<CateBean> recyclerFromString =new ArrayList<CateBean>();
                try {
                    String json = response.body().string();
                    recyclerFromString.addAll( getRecyclerFromString(json, "app-classification"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.onGetRecommendRecycler(recyclerFromString);
                    }
                });
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    //解析RecyclerView
    private List<CateBean> getRecyclerFromString (String json,String string){
        List<CateBean> list_recycle = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONObject(json).getJSONArray(string);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                CateBean cateBean = new CateBean();
                CateBean.Ext ext = new CateBean.Ext();
                cateBean.setThumb(jsonObject.optString("thumb"));
                cateBean.setTitle(jsonObject.optString("title"));
                cateBean.setSubtitle(jsonObject.optString("subtitle"));
                JSONObject jsonObject1 = jsonObject.getJSONObject("ext");
                ext.setClassification(jsonObject1.optString("classification"));
                cateBean.setExt(ext);
                list_recycle.add(cateBean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list_recycle;
    }


    //解析的ListView的
    private List<RecBean> getListFromString(String json, String string) {
        List<RecBean> list_list = new ArrayList<>();
        JSONObject jo = null;
        try {
            jo = new JSONObject(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JSONArray ja = jo.optJSONArray(string);
        for (int i = 0; i < ja.length(); i++) {
            RecBean recBean = new RecBean();
            JSONObject jo2 = ja.optJSONObject(i);
            JSONObject jo3 = jo2.optJSONObject("link_object");
            recBean.setTitle(jo3.optString("title"));
            recBean.setNick(jo3.optString("nick"));
            recBean.setAvatar(jo3.optString("avatar"));
            recBean.setThumb(jo3.optString("thumb"));
            recBean.setUid(jo3.optString("uid"));
            list_list.add(recBean);
        }
        return list_list;
    }

    //解析ViewPager的数据
    private List<BannerBean> getBannerListFromString(String json, String string){
        List<BannerBean>  list = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONObject(json).getJSONArray(string);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                BannerBean bannerBean = new BannerBean();
                BannerBean.Link_Object link_object = new BannerBean.Link_Object();
                bannerBean.setTitle(jsonObject.optString("title"));
                bannerBean.setThumb(jsonObject.optString("thumb"));
                bannerBean.setFk(jsonObject.optString("fk"));
                JSONObject link_object1 = jsonObject.getJSONObject("link_object");
                link_object.setUid(link_object1.optString("uid"));
                bannerBean.setLink_object(link_object);
                list.add(bannerBean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

}
